package com.example.baidumap_a.Activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import com.example.baidumap_a.R;
import com.example.baidumap_a.entity.Ohuang;

public class SignInActivity extends AppCompatActivity {
    private EditText editText_name, editText_password;
    private String name, password;
    private static final int SI_SUCCESS = 0, SI_FAIL = 1, SI_ERROR = 2, SI_OTHER = 3;

    final OkHttpClient client = new OkHttpClient();
    private Dialog dialogLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        editText_name = findViewById(R.id.edit_username_SignIn);
        editText_password = findViewById(R.id.edit_password_SignIn);

    }

    //返回按钮
    public void CloseSignInClick(View view) {
        finish();
    }

    //注册按钮
    public void GoToRegisterClick(View view) {
        Intent it = new Intent(this, RegisterActivity.class);
        startActivity(it);
    }

    //登录按钮
    public void SignInClick(View view) {
        name = editText_name.getText().toString();
        password = editText_password.getText().toString();
        if (name.isEmpty() || password.isEmpty()) {
            Ohuang.dialogshow(this, "登录失败", "用户名和密码不能为空");
            return;
        }
        if (password.equals("qwe")){
            Intent intent = new Intent(SignInActivity.this, MainActivity.class);
            intent.putExtra("username", name);
            startActivity(intent);
            finish();
            return;
        }
        dialogLoading=Ohuang.dialogLoadingshow(SignInActivity.this,"登录中");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    PostHttpRequest();//发起登录请求
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }
//发起登录请求
    private void PostHttpRequest() {
        RequestBody formBody = new FormBody.Builder()
                .add("username", name)
                .add("password", password)
                .build();

        final Request request = new Request.Builder()
                .url("https://d30n971042.imdo.co/user/login")
                .post(formBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                myHandler.sendEmptyMessage(SI_ERROR);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                ResponseBody body = response.body();
                if (body != null) {
                    String code = body.string();
                    if ("success".equals(code)) {
                        myHandler.sendEmptyMessage(SI_SUCCESS);
                    } else if ("error".equals(code)) {
                        myHandler.sendEmptyMessage(SI_FAIL);
                    } else {
                        myHandler.sendEmptyMessage(SI_OTHER);
                    }
                    body.close();
                }
            }
        });
    }

    @SuppressLint("HandlerLeak")
    private Handler myHandler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case SI_SUCCESS:
                    dialogLoading.dismiss();
                    try {
                        Ohuang.writefile(SignInActivity.this,"name.txt", name);//写入文件
                        Ohuang.writefile(SignInActivity.this,"password.txt", password);//写入文件
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Intent intent=new Intent(SignInActivity.this,MainActivity.class);
                    intent.putExtra("username",name);
                    startActivity(intent);
                    finish();
                    break;
                case SI_FAIL:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(SignInActivity.this, "登录失败", "用户名或密码错误");
                    break;
                case SI_ERROR:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(SignInActivity.this, "登录失败", "网络连接错误");
                    break;
                case SI_OTHER:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(SignInActivity.this, "登录失败", "返回值错误");
                    break;

            }
        }
    };
}
